package com.example.wanbaqian.activity;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.example.wanbaqian.R;
import com.example.wanbaqian.adapter.FindbankinfoListAdpter;
import com.example.wanbaqian.base.BaseActivity;
import com.example.wanbaqian.config.CommonValue;
import com.example.wanbaqian.base.HttpCallback;
import com.example.wanbaqian.config.HttpURL;
import com.example.wanbaqian.base.RefreshCallback;
import com.example.wanbaqian.bean.FindBankInfoListBean;
import com.example.wanbaqian.bean.Info;
import com.example.wanbaqian.utils.GetJsonUtils;
import com.example.wanbaqian.utils.HttpUtils;
import com.example.wanbaqian.utils.JsonUtils;
import com.example.wanbaqian.utils.SPUtils;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.wrapper.EmptyWrapper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by pengfei on 2017/8/17 0017.
 * description：<p> 借款进度</p>
 */

public class FindbankInfoActivity extends BaseActivity implements HttpCallback, RefreshCallback, MultiItemTypeAdapter.OnItemClickListener {


    private static String TAG = "FindbankInfoActivity";
    @BindView(R.id.rec_findinfo)
    RecyclerView mReclist;
    private List<FindBankInfoListBean> mList = null;
    private FindbankinfoListAdpter mAdpter;
    private EmptyWrapper emptyWrapper;

    @Override
    protected int getLayout() {
        return R.layout.activity_findbankinfo;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setStatusBarColor(R.color.main_color);
        setToolBar("进度查询");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setFindbankinfo();
    }

    /**
     * 填充数据
     */
    private void setFindbankinfo() {
        mList = new ArrayList<>();
        setHeaderAndFooter(R.id.springview, true, true, this);
        mReclist.setLayoutManager(new LinearLayoutManager(mContext));
        mAdpter = new FindbankinfoListAdpter(mContext, R.layout.item_findbank_list, mList);
        emptyWrapper = new EmptyWrapper(mAdpter);
        View emptyView = getLayoutInflater().inflate(R.layout.view_empty_activits, mReclist, false);
        TextView emptyTv = (TextView) emptyView.findViewById(R.id.emptyTv);
        emptyTv.setText("您暂无借款记录，赶快去申请吧!");
        emptyTv.setCompoundDrawablesWithIntrinsicBounds(null, ContextCompat.getDrawable(mContext, R.drawable.empty_money), null, null);
        emptyWrapper.setEmptyView(emptyView);
        mReclist.setAdapter(emptyWrapper);
        mAdpter.setOnItemClickListener(this);
    }

    @Override
    protected void initDate() {
        HttpUtils.getInstance().post(GetJsonUtils.getInstance().getRequest(HttpURL.LOAN_LIST, mContext,
                new String[]{"accessid", "cur_page", "page_size"},
                new String[]{SPUtils.getAccessid(mContext), page + "", count + ""}), mContext, HttpURL.LOAN_LIST, CommonValue.REQUEST_CODE1, this);
    }

    @Override
    public void onSuccess(String content, Info info, int requestCode) {
        springView.onFinishFreshAndLoad();
        if (!TextUtils.isEmpty(content)) {
            if (page == 1) {
                mList.clear();
            }
            List<FindBankInfoListBean> alllist = JsonUtils.getInstance().getList(content, "loan_list", FindBankInfoListBean.class);
            mList.addAll(alllist);
            emptyWrapper.notifyDataSetChanged();
            canLoadmore = JsonUtils.getInstance().havePage(content);
        }
    }

    @Override
    public void onCacheSuccess(String content, Info info, int requestCode) {

    }

    @Override
    public void onError(Info info, int requestCode) {
        showToast(info.getMsg());
    }

    /**
     * 刷新
     */
    @Override
    public void getNetDate() {
        initDate();
    }

    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }
}
